#ifndef __spthreadpool_hpp__
#define __spthreadpool_hpp__

#include "spthread.h"
#include "nb_request.h"
#include "nb_response.h"

typedef struct tagSP_Thread SP_Thread_t;
typedef void (*DispatchFunc)(NB_Request * lpRequest, NB_Response * lpResponse);

class SP_ThreadPool {
public:

	SP_ThreadPool( int maxThreads, const char * tag = 0 );
	~SP_ThreadPool();

	/// @return 0 : OK, -1 : cannot create thread
	int dispatch( DispatchFunc dispatchFunc, NB_Request * lpRequest, NB_Response * lpResponse);

	int getMaxThreads();

private:
	char * mTag;

	int mMaxThreads;
	int mIndex;
	int mTotal;
	int mIsShutdown;

	HANDLE mMainMutex;
	HANDLE mIdleCond;
	HANDLE mFullCond;
	HANDLE mEmptyCond;

	SP_Thread_t ** mThreadList;

	static unsigned _stdcall wrapperFunc( void * );
	int saveThread( SP_Thread_t * thread );
};

#endif

